﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Text;

namespace Web.Controllers
{
    public class OnlyGod : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext context)
        {
            if (!Certify.god)
                context.Result = new JsonString(new
                {
                    success = false,
                    msg = "拒绝访问，需管理权"
                });
        }
    }

    public class OnlyAdmin : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext context)
        {
            var usr = Certify.user;
            if (usr == null || !usr.admin)
                context.Result = new JsonString(new
                {
                    success = false,
                    msg = "拒绝访问，需管理员身份"
                });
        }
    }

    public class NeedLogon : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext context)
        {
            if (Certify.user == null)
                context.Result = new JsonString(new
                {
                    success = false,
                    msg = "拒绝访问，需先登录"
                });
        }
    }

    public class Myself : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext context)
        {
            var usr = Certify.user;
            if (usr == null || !usr.god &&
                usr.username.ToLower() != (context.ActionParameters["username"] ?? "").ToString().ToLower())
                context.Result = new JsonString(new
                {
                    success = false,
                    msg = "拒绝访问，越权"
                });
        }
    }

    public class OnlyTeacherUp : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext context)
        {
            if (!Certify.teacherUp)
                context.Result = new JsonString(new
                {
                    success = false,
                    msg = "拒绝访问，需教师身份或管理权"
                });
        }
    }
}
